package com.sense.kukoo.biz_media.adapter.provider

import android.view.View
import android.widget.ImageView
import android.widget.TextView
import com.google.android.material.progressindicator.CircularProgressIndicator
import com.sense.kukoo.biz_media.adapter.MediaAdapter
import com.sense.kukoo.common.R
import com.sense.kukoo.common.media.bean.LocalMediaListItemBean
import com.sense.kukoo.common.media.watcher.UserWatcher
import com.sense.kukoo.common.repo.bean.MediaItemBean
import com.sense.kukoo.common.repo.http.bean.IndexContentBean
import com.sense.kukoo.common.utils.ApplicationContext
import com.sense.kukoo.common.utils.DataTimeUtil
import com.sense.kukoo.common.wiget.MediaListItemView
import com.sense.kukoo.common.wiget.adapter.provider.MultiItemProvider
import com.sense.kukoo.common.wiget.adapter.viewholder.BaseViewHolder
import com.sense.kukoo.common.wiget.dialog.DeleteItemPopupWindow
import com.sense.kukoo.common.wiget.dialog.DeletePopupWindow
import com.sense.kukoo.lib_picture.loader.Monet

class MediaItemProvider(
    val listener: MediaAdapter.MediaListener
): MultiItemProvider<MediaItemBean>() {

    private val TAG: String = javaClass.simpleName
    private var mCurrentMusic: LocalMediaListItemBean? = null
    private var mCurrentMusicState: Int = UserWatcher.MusicStateWatcher.STATE_NOT_PLAYING
    private var mCurrentMusicProgress: Int = 0

    private val mColorGray:Int
    private val mColorRed:Int

    private var dp8 = ApplicationContext.dp2px(8F)

    private var mDp80:Float = ApplicationContext.dp2px(80F).toFloat()
    private var mDeleteBtnWith:Float = -1F

    init {
        mColorGray = ApplicationContext.getColor(R.color.dialog_btn_text_color_gray)
        mColorRed = ApplicationContext.getColor(R.color.dialog_btn_text_color_red)
    }

    override fun getItemType(): Int = MediaItemBean.TYPE_DEF

    override fun getItemLayoutId(): Int = R.layout.common_item_media_container

    override fun onBindViewHolder(viewHolder: BaseViewHolder, item: MediaItemBean) {
        val bean: LocalMediaListItemBean = item.data
        val data: IndexContentBean = bean.data
        //
        val iv_music_icon = viewHolder.getView(R.id.iv_music_icon) as ImageView
        Monet.get(iv_music_icon.context)
            .load<Any>(data.coverUrl)
            .centerCropWithConner(dp8)
            .into(iv_music_icon)
        //
        val tv_title = viewHolder.getView(R.id.tv_title) as TextView
        tv_title.text = data.title
        //
        val tv_time_left = viewHolder.getView(R.id.tv_time_left) as TextView

        //
        val iv_btn_play = viewHolder.getView(R.id.iv_btn_play) as ImageView
        val cpi_btn_progress = viewHolder.getView(R.id.cpi_btn_progress) as CircularProgressIndicator
        if(bean.idOfChapter() == (mCurrentMusic?.idOfChapter() ?: 0)){
            //当前播放的音乐
            cpi_btn_progress.visibility = View.VISIBLE
            cpi_btn_progress.progress = mCurrentMusicProgress
            when (mCurrentMusicState) {
                UserWatcher.MusicStateWatcher.STATE_PREPARE,
                UserWatcher.MusicStateWatcher.STATE_ERROR_RETRYING,
                UserWatcher.MusicStateWatcher.STATE_SEEKING,
                UserWatcher.MusicStateWatcher.STATE_PLAYING ->{
                    iv_btn_play.setImageResource(com.sense.kukoo.common_design.R.drawable.design_icon_playbutton_pause_40dp)
                }
                else ->{
                    iv_btn_play.setImageResource(com.sense.kukoo.common_design.R.drawable.design_icon_playbutton_play_40dp)
                }
            }
            //
            var len:Int = data.duration ?: 0
            len = len/60
            len = len * (100 - mCurrentMusicProgress) / 100
            len = if(len <=0 ) 1 else len
            tv_time_left.text = ApplicationContext.getString(com.sense.kukoo.common_i18n.R.string.library_min_left, len)
        } else {
            //非当前
            cpi_btn_progress.visibility = View.INVISIBLE
            iv_btn_play.setImageResource(com.sense.kukoo.common_design.R.drawable.design_icon_playbutton_start_40dp)
            //
            tv_time_left.text = DataTimeUtil.calculateTime(data.duration ?: 0)
        }
        iv_btn_play.setOnClickListener {
            listener.onMediaPlayClick(item, viewHolder.adapterPosition)
        }
        //
        val cl_card_content = viewHolder.getView(R.id.cl_card_content)
        cl_card_content.setOnLongClickListener { card ->
            val location = IntArray(2)
            card.getLocationInWindow(location)
            val locationY = location[1]
            val isUp: Boolean = locationY > 3 * card.height
            //
            DeletePopupWindow.autoShow(card.context, card, isUp){
                listener.onMediaDelete(item, viewHolder.adapterPosition)
            }
            //
            return@setOnLongClickListener true
        }
    }

    fun setView(viewHolder: BaseViewHolder, item: MediaItemBean, data: LocalMediaListItemBean, itemView: MediaListItemView){
        val data: LocalMediaListItemBean = item.data
        val itemView: MediaListItemView = viewHolder.getView(R.id.item) as MediaListItemView
        //
        itemView.setListener(object :MediaListItemView.OnMediaListItemEventListener{
            override fun onItemClick() {
                listener.onItemClick(item, viewHolder.adapterPosition)
            }

            override fun onItemLongClick(v:View) {
                val location = IntArray(2)
                v.getLocationInWindow(location)
                val locationY = location[1]
                val isUp: Boolean = locationY > 3 * v.height
                //
                DeleteItemPopupWindow.create(v.context, null)
                    .show(itemView, isUp)
                    .setListener {
                        //删除一个
                        listener.onMediaDelete(item,viewHolder.adapterPosition)
                    }
            }

            override fun onItemDeleteClick() {
                listener.onMediaDelete(item,viewHolder.adapterPosition)
            }

            override fun onBtnPlayClick() {
                listener.onMediaPlayClick(item,viewHolder.adapterPosition)
            }

            override fun onBtnToListClick() {
                listener.onMediaToList(item, viewHolder.adapterPosition)
            }

            override fun onTouchChanged(isTouching: Boolean) {
                listener.onTouchChanged(isTouching)
            }
        })
        itemView.setIcon(data.coverOfChapter())
        itemView.setTitle(data.titleOfChapter())
        //
        var duration = data.duration()
        duration = duration*1000
        itemView.setTotalTime(calculateTime(duration))
        //
        //仅有声书才显示下方的列表
        /*val isShowToList = data.type == BookBean.TYPE_AUDIOBOOK
        if (isShowToList) {
            itemView.setToListVisiable(true)
            itemView.setCollectionTitle(data.titleOfBooks())
            itemView.setNumberLeft("Ep ${data.book?.chapter ?: 1}")
            itemView.setNumberRight(" / ${data.chapterTotal ?: 0}")
        }else{
            itemView.setToListVisiable(false)
        }*/
        itemView.setToListVisiable(false)
        if(data.idOfChapter() == (mCurrentMusic?.idOfChapter() ?: 0)){
            //当前播放的音乐
            itemView.setProgressVisiable(true)
            itemView.setProgress(mCurrentMusicProgress)
            when (mCurrentMusicState) {
                UserWatcher.MusicStateWatcher.STATE_NOT_PLAYING ->{
                    itemView.setPlayIcon(R.drawable.common_icon2_play_progress)
                }
                UserWatcher.MusicStateWatcher.STATE_PREPARE,
                UserWatcher.MusicStateWatcher.STATE_ERROR_RETRYING,
                UserWatcher.MusicStateWatcher.STATE_SEEKING,
                UserWatcher.MusicStateWatcher.STATE_PLAYING ->{
                    itemView.setPlayIcon(R.drawable.common_icon2_pause_progress)
                }
                UserWatcher.MusicStateWatcher.STATE_PAUSE ->{
                    itemView.setPlayIcon(R.drawable.common_icon2_play_progress)
                }
                else ->{
                    itemView.setPlayIcon(R.drawable.common_icon2_play_progress)
                }
            }
            //
            itemView.setNumberLeftColor(mColorRed)
        }else{
            //非当前
            itemView.setProgressVisiable(false)
            itemView.setPlayIcon(R.drawable.common_icon2_play_progress)
            itemView.setNumberLeftColor(mColorGray)
        }
    }

    private fun calculateTime(milliseconds:Int): String {
        val time: Int = milliseconds / 1000

        val minute: Int
        val second: Int
        return if (time >= 60) {
            minute = time / 60
            second = time % 60
            (if (minute < 10) "0$minute" else "" + minute) + if (second < 10) ":0$second" else ":$second"
        } else {
            second = time
            if (second < 10) {
                "00:0$second"
            } else "00:$second"
        }
    }

    fun setCurrentMusic(music: LocalMediaListItemBean){
        mCurrentMusic = music
    }

    fun setCurrentPlayState(state: Int, progress: Int){
        mCurrentMusicState = state
        mCurrentMusicProgress = progress
    }

}